var loadingCode = "<div class='loading' style='text-align: center;'><img style='margin:50px auto;' src='/file/theme/homepage/img/homepage/content/ajax-loader.gif'></div>"
$(function() {
	DD_belatedPNG.fix('.png');
	setCurrentMenu();

	Vosao.initJSONRpc(loadData);

	renderSearchBox();
	renderGotoTop();
});

function loadData() {
	loadQuotation();
	loadBanner();
	loadSlide();
	loadRandomTopic();
}

function loadQuotation(){
	var currentDate = new Date();
	var quoteUrl = "/include/quotations/" + currentDate.getDate();
	var structName = "quotations";
	
	Vosao.jsonrpc.frontPageFrontService.renderStructureContent(function(r){
		if(r != ''){
			$('#quote').html(r);
		}
		$('#quote').removeClass('loading');
	}, quoteUrl, structName);
}

function loadBanner(){
	var bannerPath  =  "/include/banner";
	Vosao.jsonrpc.frontPageFrontService.findContent(function(r){
		if(r != ''){
			$('#banner').html(r);
		}
	},bannerPath);
}

function loadSlide(){
	var technologyPath  = "/include/bannerhome/technology";
	loadBannerContent(technologyPath);
}

function loadBannerContent(pathContent){
	$('#tab-content').html(loadingCode);
	Vosao.jsonrpc.frontPageFrontService.findContent(function(r){
		if(r != ''){
			$('#tab-content').html(r);
		}
	}, pathContent);
}

function loadRandomTopic() {
	"#random-left"
	"#random-right"
}
//TODO:home new
//<div class="sub">
//<div class="pd10"></div>
//</div>
//<div class="clear"></div>

//Banner home action
$('ul#tab-head li a').live('click', function(e) {
    $(this).parent().attr('class','current');
    $(this).parent().siblings().attr('class','normal'); 
    var tab = $(this).attr("href");
   
    var path = "/include/bannerhome/technology";
    
    if(tab == '#tab-2'){
    	path = "/include/bannerhome/e-commerce";
    }else if(tab == '#tab-3'){
    	path = "/include/bannerhome/software";
    }else if(tab == '#tab-4'){
    	path = "/include/bannerhome/review";
    }
    alert(tab);
    loadBannerContent(path);
    return false;
});  

// search script
$('#quick-search').submit(function() {
	onSearch();
	return false;
});
$('#search').focus(function() {
	if ($('#search').val() == 'Từ khóa tìm kiếm') {
		$('#search').val('');
	}
});
$('#search').blur(function() {
	if (!$('#search').val()) {
		$('#search').val('Từ khóa tìm kiếm');
	}
});
function onSearch() {
	var query = $('#search').val();
	if (query) {
		location.href = '/search?query=' + query;
	}
}

//set current for menu item by pathname
function setCurrentMenu() {
	var menu = $("ul#menu li a");
	for ( var i = 0; i < menu.size(); i++) {
		var menuItem = $(menu[i]).attr("href").substring(1);
		var path = location.pathname;
		var currentPath = path.substring(1).split('/');
		if (menuItem == currentPath[0]) {
			$(menu[i]).attr("class", "current");
		}
	}
}

//Goto top
function renderGotoTop() {
	$('#page-container').append('<div id="go-to-top">Back to Top</div>');

	$('#go-to-top').hide();

	$(window).scroll(function() {
		if ($(window).scrollTop() != 0) {
			$('#go-to-top').fadeIn();
		} else {
			$('#go-to-top').fadeOut();
		}
	});

	$('#go-to-top').click(function() {
		$('html, body').animate({
			scrollTop : 0
		}, 500);
	});
}

//search box
function renderSearchBox(){
	$('#search-icon').toggle(function() {
		$('#search-box').fadeIn('slow');
	}, function() {
		$('#search-box').fadeOut('fast');
	});
}